﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.UI;
using AG.Editor.TerrainUI.Windows;
using AG.Editor.Core;
using AG.Editor.Core.Data;
using AG.Editor.TerrainUI.Controls;

namespace AG.Editor.TerrainUI
{
    public partial class AGTerrainMgrPanel : UserControl, IAGEMainComponent
    {
        private AGTerrainPreviewPanel _previewPanel;

        public AGTerrainMgrPanel()
        {
            InitializeComponent();

            BindingTerrainTree();

            _previewPanel = new AGTerrainPreviewPanel();
            _previewPanel.Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(_previewPanel);
        }

        public void OnActived(AGEMainMenuMidiator mmm)
        {
            mmm.Clear();

            ToolStripMenuItem m1 = mmm.AddMenu(new ToolStripMenuItem("地形管理"));
            ToolStripMenuItem miCreateTerrain = new ToolStripMenuItem("创建地形");
            miCreateTerrain.Click += new EventHandler(miCreateTerrain_Click);
            m1.DropDownItems.Add(miCreateTerrain);
            ToolStripMenuItem miRemoveTerrain = new ToolStripMenuItem("删除地形");
            miRemoveTerrain.Click += new EventHandler(miRemoveTerrain_Click);
            m1.DropDownItems.Add(miRemoveTerrain);

            ToolStripMenuItem miEditModel = new ToolStripMenuItem("修改模型");
            //miEditModel.Click += new EventHandler(miEditModel_Click);
            //m1.DropDownItems.Add(miEditModel);

            ToolStripMenuItem miRemoveModel = new ToolStripMenuItem("删除模型");
            //miRemoveModel.Click += new EventHandler(miRemoveModel_Click);
            //m1.DropDownItems.Add(miRemoveModel);
        }

        void miCreateTerrain_Click(object sender, EventArgs e)
        {
            AGCreateTerrainWindow window = new AGCreateTerrainWindow();
            if (window.ShowDialog() == DialogResult.OK)
            {
                AGEContext.Current.EProject.AddTerrain(window.Terrain);
                BindingTerrainTree();
            }
        }

        void miRemoveTerrain_Click(object sender, EventArgs e)
        {
            if(treeView1.SelectedNode == null || treeView1.SelectedNode.Tag == null)
            {
                MessageBox.Show("请选择需要删除的地形");
                return;
            }
            AGTerrain selTerrain = treeView1.SelectedNode.Tag as AGTerrain;
            if (MessageBox.Show(string.Format("是否要删除地形[{0}]?", selTerrain), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }

            AGEContext.Current.EProject.RemoveTerrain(selTerrain);
            BindingTerrainTree();
        }

        private void BindingTerrainTree()
        {
            treeView1.Nodes.Clear();
            //
            // 获取单一地形
            for (int index = 0; index < AGEContext.Current.EProject.Terrains.Count; index++)
            {
                AGTerrain terrain = AGEContext.Current.EProject.Terrains[index];
                
                TreeNode node = new TreeNode();
                node.Text = terrain.ToString();
                node.Tag = terrain;
                treeView1.Nodes.Add(node);

                for (int tileIndex = 0; tileIndex < terrain.Tiles.Count; tileIndex++)
                {
                    AGTerrainTile tile = terrain.Tiles[tileIndex];

                    TreeNode tileNode = new TreeNode();
                    tileNode.Text = tile.ToString();
                    tileNode.Tag = tile;
                    node.Nodes.Add(tileNode);
                }
            }
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            AGTerrain selTerrain = e.Node.Tag as AGTerrain;
            if (selTerrain != null)
            {
                _previewPanel.SetTerrain(selTerrain);
            }
        }
    }
}
